package cn.edu.sptc.sms_luojie.controller;
import cn.edu.sptc.sms_luojie.dao.MajorMapper;
import cn.edu.sptc.sms_luojie.entity.Major;
import cn.edu.sptc.sms_luojie.entity.Student;
import cn.edu.sptc.sms_luojie.service.MajoyService;
import cn.edu.sptc.sms_luojie.service.StudentService;
import cn.edu.sptc.sms_luojie.service.impl.MajorServicelmpl;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.sql.DataTruncation;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/student")
@Tag(name="学生管理",description = "学生的CRUD(增删查改)操作")
public class StudentController {
    @Autowired
    private StudentService studentService;

    //    @GetMapping("/query/{id}")
//    public Major queryMajorByID(@PathVariable String id){
//        return majoyService.querByID(Long.parseLong(id));
//    }
    @GetMapping("/query/{id}")
    @Parameters(
            @Parameter(name = "id", description = "学生ID",required = true,example = "1",in = ParameterIn.QUERY)
    )
    @Operation(summary = "查询",description = "根据学生id查询单个专业信息（路径参数形式）")
    @ApiResponses({
            @ApiResponse(responseCode = "200" ,description = "成功"),
            @ApiResponse(responseCode = "500" ,description = "服务器错误"),
            @ApiResponse(responseCode = "404" ,description = "id有误")
    })
    public Map<String, Object> queryMajorByID(@PathVariable String id) {
        Map map = new HashMap();
        map.put("code", "200");
        map.put("msg", "成功");
        map.put("data", studentService.queryByID(Long.parseLong(id)));
        return map;
    }

    @GetMapping("/query")
    @Parameters(
            @Parameter(name = "id", description = "专业ID",required = true,example = "1",in = ParameterIn.QUERY)
    )
    @Operation(summary = "查询",description = "根据学生id查询单个专业信息（表单参数形式）")
    @ApiResponses({
            @ApiResponse(responseCode = "200" ,description = "成功"),
            @ApiResponse(responseCode = "500" ,description = "服务器错误"),
            @ApiResponse(responseCode = "404" ,description = "id有误")
    })
    public Map<String, Object> queryStudentByIDParam() {
        Map map = new HashMap();
        map.put("code", "200");
        map.put("msg", "成功");
        map.put("data", studentService.queryByAll());
        return map;
    }

    @GetMapping("/queryAll")
    @Operation(summary = "查询",description = "查询所有学生信息")
    @ApiResponses({
            @ApiResponse(responseCode = "200" ,description = "成功"),
            @ApiResponse(responseCode = "500" ,description = "服务器错误")
    })
    public Map<String, Object> queryMajorByAll(@PathVariable String id) {
        Map map = new HashMap();
        map.put("code", "200");
        map.put("msg", "成功");
        map.put("data", studentService.queryByAll());
        return map;
    }

    @PostMapping(value = "/save1", consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({
            @ApiResponse(responseCode = "200" ,description = "成功"),
            @ApiResponse(responseCode = "500" ,description = "服务器错误")
    })
    @io.swagger.v3.oas.annotations.parameters.RequestBody(description = "传入学生信息，只需要传入学生姓名，性别，专业，学生id,创建和更新时间不需要", required = true, content = @Content(schema = @Schema(implementation = Student.class)))
    @Operation(summary = "插入",description = "通过json格式插入信息")
    public Map<String, Object> insert(@RequestBody  Student student) {
        Map<String, Object> map = new HashMap<>();
        student.setCreateTime(new Date());
        student.setUpdateTime(new Date());
        if (studentService.insertStudent(student)) {
            map.put("code", "200");
            map.put("msg", "添加成功");
        } else {
            map.put("code", "201");
            map.put("msg", "添加失败");
        }
        return map;
    }
    @PostMapping(value = "/save2", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    @Operation(summary = "插入",description = "通过表格格式插入信息")
    @ApiResponses({
            @ApiResponse(responseCode = "200" ,description = "成功"),
            @ApiResponse(responseCode = "500" ,description = "服务器错误")
    })
    @io.swagger.v3.oas.annotations.parameters.RequestBody(
            description = "传入学生信息，只需要传入学生姓名，性别，专业，学生id,创建和更新时间不需要",
            required = true,
            content = @Content(schema = @Schema(implementation = Student.class)))
    public Map<String, Object> insertByform(  Student student) {
        Map<String, Object> map = new HashMap<>();
        student.setCreateTime(new Date());
        student.setUpdateTime(new Date());
        if (studentService.insertStudent(student)) {
            map.put("code", "200");
            map.put("msg", "添加成功");
        } else {
            map.put("code", "201");
            map.put("msg", "添加失败");
        }
        return map;
    }
    @PostMapping(value = "/update", consumes = MediaType.APPLICATION_JSON_VALUE)
    @Operation(summary = "更新",description = "通过json格式更新信息")
    @ApiResponses({
            @ApiResponse(responseCode = "200" ,description = "成功"),
            @ApiResponse(responseCode = "500" ,description = "服务器错误"),
            @ApiResponse(responseCode = "404" ,description = "id有误")
    })
    public Map<String, Object> updateByjosn(@RequestBody Student student) {
        Map<String, Object> map = new HashMap<>();
        student.setUpdateTime(new Date());
        if (studentService.updateStudent(student)) {
            map.put("code", "200");
            map.put("msg", "更新成功");
        } else {
            map.put("code", "201");
            map.put("msg", "更新失败");
        }
        return map;
    }
    @PostMapping(value = "/update", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    @Operation(summary = "更新",description = "通过表格格式更新信息")
    @ApiResponses({
            @ApiResponse(responseCode = "200" ,description = "成功"),
            @ApiResponse(responseCode = "500" ,description = "服务器错误"),
            @ApiResponse(responseCode = "404" ,description = "id有误")
    })
    public Map<String, Object> updateByfrom( Student student) {
        Map<String, Object> map = new HashMap<>();
        student.setUpdateTime(new Date());
        if (studentService.updateStudent(student)) {
            map.put("code", "200");
            map.put("msg", "更新成功");
        } else {
            map.put("code", "201");
            map.put("msg", "更新失败");
        }
        return map;
    }
    @GetMapping("/delete/{id}")
    @Operation(summary = "删除",description = "通过id号删除信息")
    @Parameters(
            @Parameter(name = "id", description = "学生ID",required = true,example = "1",in = ParameterIn.QUERY)
    )
    @ApiResponses({
            @ApiResponse(responseCode = "200" ,description = "成功"),
            @ApiResponse(responseCode = "500" ,description = "服务器错误"),
            @ApiResponse(responseCode = "404" ,description = "id有误")
    })
    public Map<String, Object> deletStudentById(@PathVariable String id) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", "200");
        map.put("message", "删除成功");
        map.put("data", studentService.deleteStudent(Long.parseLong(id)));
        return map;
    }
    @GetMapping("/delete")
    @Operation(summary = "删除",description = "通过id号删除信息")
    @Parameters(
            @Parameter(name = "id", description = "学生ID",required = true,example = "1",in = ParameterIn.QUERY)
    )

    @ApiResponses({
            @ApiResponse(responseCode = "200" ,description = "成功"),
            @ApiResponse(responseCode = "500" ,description = "服务器错误"),
            @ApiResponse(responseCode = "404" ,description = "id有误")
    })
    public Map<String, Object> deleteStudentByIDParm(@RequestParam String id) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", "200");
        map.put("message", "删除成功");
        map.put("data", studentService.deleteStudent(Long.parseLong(id)));
        return map;
    }


}